import { themes as prismThemes } from 'prism-react-renderer';
import type { Config } from '@docusaurus/types';
import type * as Preset from '@docusaurus/preset-classic';
import type { ScalarOptions } from '@scalar/docusaurus';

const config: Config = {
  title: 'Gitee AI',
  tagline: '开发 AI 应用从此简单',
  favicon: 'img/favicon.ico',

  // Set the production url of your site here
  url: 'https://ai.gitee.com',
  // Set the /<baseUrl>/ pathname under which your site is served
  // For GitHub pages deployment, it is often '/<projectName>/'
  baseUrl: '/docs/',

  // GitHub pages deployment config.
  // If you aren't using GitHub pages, you don't need these.
  organizationName: 'gitee-ai', // Usually your GitHub org/user name.
  projectName: 'docs', // Usually your repo name.

  onBrokenLinks: 'throw',
  onBrokenMarkdownLinks: 'warn',

  // Even if you don't use internationalization, you can use this field to set
  // useful metadata like html lang. For example, if your site is Chinese, you
  // may want to replace "en" with "zh-Hans".
  i18n: {
    defaultLocale: 'zh-Hans',
    locales: ['zh-Hans'],
  },

  presets: [
    [
      'classic',
      {
        docs: {
          routeBasePath: '/',
          sidebarPath: './sidebars.ts',
          // Please change this to your repo.
          // Remove this to remove the "edit this page" links.
          editUrl: 'https://gitee.com/gitee-ai/docs/edit/main/',
        },
        blog: {
          showReadingTime: true,
          // Please change this to your repo.
          // Remove this to remove the "edit this page" links.
          editUrl: 'https://gitee.com/gitee-ai/docs/edit/main/',
        },
        theme: {
          customCss: './src/css/custom.css',
        },
      } satisfies Preset.Options,
    ],
  ],

  themeConfig: {
    // Replace with your project's social card
    // image: 'img/docusaurus-social-card.jpg',
    navbar: {
      // title: 'Gitee AI',
      logo: {
        alt: 'Gitee AI',
        src: 'img/logo-full.svg',
        srcDark: 'img/logo-full-white.svg',
        href: 'https://ai.gitee.com',
        target: '_self',
      },
      items: [
        {
          label: '模型',
          to: 'https://ai.gitee.com/models',
          target: '_self',
        },
        {
          label: '数据集',
          to: 'https://ai.gitee.com/datasets',
          target: '_self',
        },
        {
          label: '应用',
          to: 'https://ai.gitee.com/apps',
          target: '_self',
        },
        {
          label: 'Serverless API',
          to: 'https://ai.gitee.com/serverless-api',
          target: '_self',
        },
        {
          label: '解决方案',
          items: [
            {
              label: '模型引擎',
              to: 'https://ai.gitee.com/endpoints',
              target: '_self',
            },
            {
              label: '算力网络',
              to: 'https://ai.gitee.com/computing-network',
              target: '_self',
            },
            {
              label: 'Serverless 推理引擎',
              to: 'https://ai.gitee.com/serverless',
              target: '_self',
            },
          ],
        },
        {
          type: 'docSidebar',
          sidebarId: 'tutorialSidebar',
          position: 'left',
          label: '文档',
        },
        {
          type: 'dropdown',
          label: '接口文档',
          position: 'left',
          items: [
            {
              label: '推理接口',
              to: '/docs/openapi/serverless',
            },
            {
              label: '平台接口',
              to: '/docs/openapi/v1',
            },
            {
              label: '支付接口',
              to: '/docs/openapi/payment',
            },
          ],
        },
        {
          label: 'AI 客服',
          to: '/docs/金牌客服马建仓',
          target: '_self',
        },
      ],
    },
    footer: {
      style: 'light',
      /*
      links: [
        {
          title: 'Docs',
          items: [
            {
              label: 'Tutorial',
              to: '/docs/intro',
            },
          ],
        },
        {
          title: 'Community',
          items: [
            {
              label: 'Stack Overflow',
              href: 'https://stackoverflow.com/questions/tagged/docusaurus',
            },
            {
              label: 'Discord',
              href: 'https://discordapp.com/invite/docusaurus',
            },
            {
              label: 'Twitter',
              href: 'https://twitter.com/docusaurus',
            },
          ],
        },
        {
          title: 'More',
          items: [
            {
              label: 'Blog',
              to: '/blog',
            },
            {
              label: 'Gitee AI',
              href: 'https://ai.gitee.com',
            },
          ],
        },
      ],
      */
      copyright: `Copyright © ${new Date().getFullYear()} Gitee AI`,
    },
    prism: {
      theme: prismThemes.github,
      darkTheme: prismThemes.dracula,
    },
    zoom: {
      selector: '.markdown img',
      config: {},
    },
  } satisfies Preset.ThemeConfig,
  plugins: [
    'docusaurus-plugin-image-zoom',
    [
      '@scalar/docusaurus',
      {
        id: 'openapi/serverless',
        label: '',
        route: '/docs/openapi/serverless',
        configuration: {
          spec: {
            url: 'https://ai.gitee.com/api/serverless/yaml',
          },
          hideModels: true,
          servers: [
            {
              url: 'https://ai.gitee.com/api/serverless',
              description: '生产服务器',
            },
          ],
        },
      } as ScalarOptions,
    ],
    [
      '@scalar/docusaurus',
      {
        id: 'openapi/v1',
        label: '',
        route: '/docs/openapi/v1',
        configuration: {
          spec: {
            url: 'https://ai.gitee.com/api/v1/yaml',
          },
          hideModels: true,
          servers: [
            {
              url: 'https://ai.gitee.com/api/v1',
              description: '生产服务器',
            },
          ],
        },
      } as ScalarOptions,
    ],
    [
      '@scalar/docusaurus',
      {
        id: 'openapi/payment',
        label: '',
        route: '/docs/openapi/payment',
        configuration: {
          spec: {
            url: 'https://ai.gitee.com/api/pay/yaml',
          },
          hideModels: true,
          servers: [
            {
              url: 'https://ai.gitee.com/api/pay',
              description: '生产服务器',
            },
          ],
        },
      } as ScalarOptions,
    ],
    [
      'docusaurus-plugin-remote-content',
      {
        name: 'gai-readme',
        sourceBaseUrl: 'https://gitee.com/gitee-ai/cli/raw/master/',
        outDir: 'docs/gai',
        documents: ['README.md'],
        modifyContent: (filename, content) => {
          if (filename === 'README.md') {
            let lines = content.split('\n');

            // 修改标题
            const titleIndex = lines.findIndex((line) =>
              line.trim().includes('# Gitee AI 命令行工具'),
            );
            if (titleIndex !== -1) {
              lines[titleIndex] = '# 使用 Gitee AI 命令行工具上传模型';
            }

            // update img link
            lines = lines.map((line) =>
              line.includes('png')
                ? line.replace('assets/', '../../static/img/gai/')
                : line,
            );

            // 参与贡献之后的行都不要
            const idx = lines.findIndex((line) => line.includes('参与贡献'));
            if (idx !== -1) {
              lines = lines.slice(0, idx);
            }

            return {
              filename: 'gai.md',
              content: lines.join('\n'),
            };
          }
          return undefined;
        },
      },
    ],
  ],
};

export default config;
